use std::path::Path;

#[allow(dead_code)]
pub fn infer_file_type_from_name(name: &str) -> Option<String> {
    let path = Path::new(name);
    let extension = match path.extension() {
        Some(ext) => ext.to_str()?.to_lowercase(),
        None => return Some("OTHER".to_string()),
    };

    match extension.as_str() {
        "csv" => Some("CSV".to_string()),
        "json" => Some("JSON".to_string()),
        "jsonl" => Some("JSONL".to_string()),
        "pdf" => Some("PDF".to_string()),
        "xlsx" | "xls" => Some("EXCEL".to_string()),
        _ => Some("OTHER".to_string()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_infer_file_type() {
        assert_eq!(
            infer_file_type_from_name("data.csv"),
            Some("CSV".to_string())
        );
        assert_eq!(
            infer_file_type_from_name("config.json"),
            Some("JSON".to_string())
        );
        assert_eq!(
            infer_file_type_from_name("document.pdf"),
            Some("PDF".to_string())
        );
        assert_eq!(
            infer_file_type_from_name("data.jsonl"),
            Some("JSONL".to_string())
        );
        assert_eq!(
            infer_file_type_from_name("spreadsheet.xlsx"),
            Some("EXCEL".to_string())
        );
        assert_eq!(
            infer_file_type_from_name("old.xls"),
            Some("EXCEL".to_string())
        );
        assert_eq!(
            infer_file_type_from_name("unknown.txt"),
            Some("OTHER".to_string())
        );
        assert_eq!(
            infer_file_type_from_name("no_extension"),
            Some("OTHER".to_string())
        );
        assert_eq!(
            infer_file_type_from_name("UPPERCASE.CSV"),
            Some("CSV".to_string())
        );
    }
}
